<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="handleInput"
        />
        <!-- 组织架构树形组件 -->
        <!-- node-key="唯一且不重复的数据"
             node-key="id" 表示树形结构的每一个节点，都使用id数据做唯一标识(推荐)
             node-key="name" 表示树形结构的每一个节点，都使用name数据做唯一标识
             node-key="code" 表示树形结构的每一个节点，都使用code数据做唯一标识
        -->
        <el-tree
          ref="deptTree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          :expand-on-click-node="false"
          default-expand-all
          highlight-current
          @current-change="selectNode"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini">excel导入</el-button>
          <el-button size="mini">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employeeList">
          <el-table-column prop="staffPhoto" label="头像" width="80px" align="center">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :size="30" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username?.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>其他</span>
              <!-- <span>{{ row.formOfEmployment === 1 ? '正式' : row.formOfEmployment === 2 ? '非正式' : '其他' }}</span> -->
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作">
            <template>
              <el-button type="text" size="mini">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-button type="text" size="mini">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" type="flex" justify="end" align="middle">
          <el-pagination
            layout="total, prev, pager, next"
            :total="queryParams.total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="hadleCurrentCange"
          />
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartmentsAPI } from '@/api/department'
import { transListToTreeData } from '@/utils'
import { exportExcelAPI, getEmployeesAPI } from '@/api/employee'
import FileSaver from 'file-saver'
export default {
  name: 'Employee',
  data() {
    return {
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 获取部门员工查询参数对象：属性必须跟接口文档一致
      queryParams: {
        departmentId: null, // 记录部门id
        total: 0,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      employeeList: []
    }
  },
  created() {
    // 获取组织架构部门列表数据
    this.getDepartmentList()
  },
  methods: {
    async exportExcelClick() {
      const result = await exportExcelAPI()
      FileSaver.saveAs(result, '员工列表.xlsx')
    },

    hadleCurrentCange(currentPage) {
      this.queryParams.page = currentPage
      this.getEmployeeList()
    },
    // 关键字搜索框
    handleInput() {
      // 防抖
      clearTimeout(this.timer)
      // 开启定时器
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 300)
    },
    // 专门封装获取员工列表数据的方法：谁高亮谁调用
    async getEmployeeList() {
      // 调用员工列表接口方法
      const { rows, total } = await getEmployeesAPI(this.queryParams)
      this.employeeList = rows
      this.queryParams.total = total
    },
    // 组织架构树形节点被点击的事件：点击哪一个部门，就记录哪一个部门的ID
    selectNode(nodeData) {
      // console.log(nodeData)
      this.queryParams.departmentId = nodeData.id
      // 点击哪一个部门，就获取哪一个部门的员工
      this.getEmployeeList()
    },
    // 调用组织架构部门列表接口方法
    async getDepartmentList() {
      // 响应式数据是异步刷新的，这句代码没有执行完，后面的setCurrentKey就执行了
      this.depts = transListToTreeData(await getDepartmentsAPI())
      // 记录传智教育的id
      this.queryParams.departmentId = this.depts[0].id
      // 需要将<传智教育>的节点ID传入给 setCurrentKey()
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.depts[0].id)
      })
      // 默认获取传智教育部门员工数据
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
